package tabletree;

import java.util.ArrayList;

import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import filesystem.*;

public class FileSystemAdapter extends DefaultTreeSelectionModel implements TreeModel, TableModel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -9155699909084481105L;
	
	
	protected EventListenerList treeListeners = new EventListenerList();
	protected EventListenerList tableListeners = new EventListenerList();
	
	private FileSystem m_filesystem;
	private String[] m_columnNames = {"Name", "Type", "Size", "Owner", "Created" };
	private Object[][] m_data;
	
	public FileSystemAdapter(FileSystem filesystem)
	{
		m_filesystem = filesystem;
		m_filesystem.setAdapter(this);
		m_data = getCurrent().getChildrenInfo();
	}
	
	public void setCurrent(Object o)
	{
		Directory current = (Directory)o;
		m_filesystem.setCurrent(current);	
	}
	
	public Directory getCurrent()
	{
		return m_filesystem.getCurrent();
	}
	
	public void SetData()
	{
		m_data = getCurrent().getChildrenInfo();
	}

	@Override
	public void addTreeModelListener(TreeModelListener listener) 
	{
		treeListeners.add(TreeModelListener.class, listener);
	}

	@Override
	public Object getChild(Object parent, int position) 
	{
		if (parent instanceof Directory)
		{
			Directory dir = (Directory)parent;
			return dir.getChildren().get(position);
		}
		
		return 0;
	}

	@Override
	public int getChildCount(Object parent) 
	{
		if (parent instanceof Directory)
		{
			Directory dir = (Directory)parent;
			return dir.getChildren().size();
		}
		
		return 0;
	}

	@Override
	public int getIndexOfChild(Object parent, Object child) 
	{
		if (parent instanceof Directory && child instanceof FSElement)
		{
			Directory dir = (Directory)parent;
			ArrayList<FSElement> children = dir.getChildren();
			for (int i = 0; i<children.size(); i++)			
				if (child.equals(children.get(i)))
					return i;			
		}
		return 0;
	}

	@Override
	public Object getRoot() 
	{		
		return m_filesystem.getRoot();
	}

	@Override
	public boolean isLeaf(Object filesystemelement) 
	{		
		return ((FSElement)filesystemelement).isLeaf();
	}

	@Override
	public void removeTreeModelListener(TreeModelListener listener) 
	{
		treeListeners.remove(TreeModelListener.class, listener);
	}

	@Override
	public void valueForPathChanged(TreePath arg0, Object arg1) 
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void addTableModelListener(TableModelListener l) 
	{
		tableListeners.add(TableModelListener.class, l);
		
	}

	@Override
	public Class getColumnClass(int arg0) 
	{
		return getValueAt(0, arg0).getClass();

	}

	@Override
	public int getColumnCount() 
	{		
		return m_columnNames.length;
	}

	@Override
	public String getColumnName(int i) 
	{
		return m_columnNames[i];		
	}

	@Override
	public int getRowCount() 
	{		
		return getCurrent().getChildren().size();
	}

	@Override
	public Object getValueAt(int row, int column) 
	{		
		return m_data[row][column];
	}

	@Override
	public boolean isCellEditable(int arg0, int arg1) 
	{		
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) 
	{
		tableListeners.remove(TableModelListener.class, l);		
	}

	@Override
	public void setValueAt(Object arg0, int arg1, int arg2) 
	{
		// TODO Auto-generated method stub
		
	}

	public void fireTableChanged(TableModelEvent e) 
	{
		int index;
	    TableModelListener listener;
	    Object[] list = tableListeners.getListenerList();
	   
	    for (index = 0; index < list.length; index += 2)
        {
          listener = (TableModelListener) list [index + 1];
          listener.tableChanged(e);
        }
	}	
}
